package com.wngbms.aop.interceptor;

import com.wngbms.aop.staticproxy.DefaultEchoService;
import com.wngbms.aop.staticproxy.EchoService;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class AopInterceptorDemo {

    public static void main(String[] args) {

//        AopInterceptor aopInterceptor = new AopInterceptor(new DefaultEchoService(), new BeforeInterceptor() {
        AopInterceptor aopInterceptor = new AopInterceptor(new ExpEchoService(), new BeforeInterceptor() {
            @Override
            public Object before(Object proxy, Method method, Object[] args) {
                System.out.println("before");
                return System.currentTimeMillis();
            }
        }, new AfterReturnInterceptor() {
            @Override
            public Object after(Object proxy, Method method, Object[] args, Object returnObj) {
                System.out.println("after");
                return System.currentTimeMillis();
            }
        }, new FinallyInterceptor() {
            @Override
            public Object finalize(Object proxy, Method method, Object[] args, Object resultObj) {
                System.out.println("finalize");
                return 999L;
            }
        }, new ExceptionInterceptor() {
            @Override
            public void intercept(Object proxy, Method method, Object[] args, Throwable throwable) {
                System.err.println("exp");
                throwable.printStackTrace();
            }
        });

        Object proxyInstance = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{EchoService.class}, aopInterceptor);
        EchoService echoService = (EchoService) proxyInstance;
        System.out.println(echoService.echo("miui"));
    }

}
